package com.csi.service;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.jwt.JWTPayload;
import cn.hutool.jwt.JWTUtil;
import com.csi.utils.ReturnResults;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class TokenService {
    //利用hutool工具生成token
    public String generateHotelToken(long employeeId, String userName, long roleId, long hotelId) {
        System.out.println(employeeId);
        DateTime now = DateTime.now();
        DateTime newTime = now.offsetNew(DateField.MINUTE, 60);

        Map<String, Object> payload = new HashMap<String, Object>();
        //签发时间
        payload.put(JWTPayload.ISSUED_AT, now);
        //过期时间
        payload.put(JWTPayload.EXPIRES_AT, newTime);
        //生效时间
        payload.put(JWTPayload.NOT_BEFORE, now);
        //载荷
        payload.put("employeeId", employeeId);
        payload.put("userName", userName);
        payload.put("roleId", roleId);
        payload.put("hotelId", hotelId);

        String key = ReturnResults.SECRET_KEY;
        return JWTUtil.createToken(payload, key.getBytes());
    }

    public String UserLoginToken(long userId,String userNickname) {
        DateTime now = DateTime.now();
        DateTime newTime = now.offsetNew(DateField.MINUTE, 60);

        Map<String, Object> payload = new HashMap<String, Object>();
        //签发时间
        payload.put(JWTPayload.ISSUED_AT, now);
        //过期时间
        payload.put(JWTPayload.EXPIRES_AT, newTime);
        //生效时间
        payload.put(JWTPayload.NOT_BEFORE, now);
        //载荷
        payload.put("userNickname", userNickname);
        payload.put("userId", userId);
        String key = ReturnResults.SECRET_KEY;
        return JWTUtil.createToken(payload, key.getBytes());
    }
}
